/*
 * ExportadorSTL.java
 *
 * Created on 19 de Outubro de 2007, 08:08
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package br.com.cpqd.ssar.utilitarias;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;


/**
 *
 * @author rafaelbuck
 */
public class ExportadorSTL {
    
    private String STR_SOLID    = new String("solid");
    private String STR_ENDSOLID = new String("endsolid");
    private String STR_FACET    = new String("facet");
    private String STR_ENDFACET = new String("endfacet");
    private String STR_LOOP     = new String("loop");
    private String STR_ENDLOOP  = new String("endloop");
    private String STR_NORMAL   = new String("normal");
    private String STR_OUTER    = new String("outer");
    private String STR_VERTEX   = new String("vertex");
    
    /**
     * Creates a new instance of ExportadorSTL
     */
    public ExportadorSTL(Triangulo[] obj, String arq) {
    
        try{
            PrintWriter pw = new PrintWriter(new FileWriter(arq + ".stl"));
            pw.println(STR_SOLID + " BR_FDTD_object");
            
            for(int i=0; i < obj.length; i++){
                pw.print(STR_FACET + " " + STR_NORMAL + " ");
                pw.printf("%.6e ",obj[i].getNormal()[0]);
                pw.printf("%.6e ",obj[i].getNormal()[1]);
                pw.printf("%.6e \n",obj[i].getNormal()[2]);
                
                pw.println(STR_OUTER + " " + STR_LOOP);
                
                pw.print(STR_VERTEX + " ");
                pw.printf("%.6e ",obj[i].getVertice(0).getCoordenada(0));
                pw.printf("%.6e ",obj[i].getVertice(0).getCoordenada(1));
                pw.printf("%.6e \n",obj[i].getVertice(0).getCoordenada(2));
                pw.print(STR_VERTEX + " ");
                pw.printf("%.6e ",obj[i].getVertice(1).getCoordenada(0));
                pw.printf("%.6e ",obj[i].getVertice(1).getCoordenada(1));
                pw.printf("%.6e \n",obj[i].getVertice(1).getCoordenada(2));
                pw.print(STR_VERTEX + " ");
                pw.printf("%.6e ",obj[i].getVertice(2).getCoordenada(0));
                pw.printf("%.6e ",obj[i].getVertice(2).getCoordenada(1));
                pw.printf("%.6e \n",obj[i].getVertice(2).getCoordenada(2));

                pw.println(STR_ENDLOOP);
                
                pw.println(STR_ENDFACET);
            }
            
            pw.print(STR_ENDSOLID + " BR_FDTD_object");
            
            pw.close(); // Without this, the output file may be empty
        }catch(IOException e){
            System.out.println("Erro ao gravar" + arq + ".stl");
        }
    
    }
    
}
